<?php

class Article extends Controller {
	
	/**
	 * Constructor
	 */
	function Article(){
		parent::Controller();
		$this->load->model('Article_model');
	}
	
	/**
	 * Add a new article to the database
	 */
	function add_article(){
		
		if ($this->auth->has_rights_to("add","article"))
		{
			$infos = array(
	    					'writer' => $this->auth->get_user_id(),
	    					'title' => $this->input->post('title'),
							'content' => $this->input->post('text_content')
			);
			
			$result=$this->Article_model->new_article($infos);
			$this->load->view('article/addOk', $infos);
		}
		else
		{
     		$this->load->view('forbidden');
		}
	}
	
	/**
	 * Display the add article form
	 */
	function add(){
		
		if ($this->auth->has_rights_to("add","article"))
		{
			$this->load->view('article/add');
		}
		else
		{
     		$this->load->view('forbidden');
		}
	}
	
	
	/**
	 * Display the article list for the user $user_id
	 */
	function userArticlelist($user_id, $page=0){
		if ($this->auth->has_rights_to("list","article") )
		{
			if ($page < 0) { $page = 0;}
		
			$this->load->library('paginationajax');
			
			$config['base_url'] = site_url('article/userArticlelist/'.$user_id.'/');
			$config['total_rows'] = $this->Article_model->get_article_count(array('writer' => $user_id));
			$config['per_page'] = '2';
			$config['uri_segment'] = '4';
			$config['js_function'] = 'update_news';
		
			$data['articlelist']=$this->Article_model->get_articles($page, $config['per_page'], array('writer' => $user_id));		
			$this->paginationajax->initialize($config);
			$data['pagination']=$this->paginationajax->create_links();
			$data['short']=true;
			$data['articletitle']= 'Your news';
			$this->load->view('article/list',$data);
		}
		else
		{
     		$this->load->view('forbidden');
		}
	}
	
	/**
	 * Display the article list
	 */
	function articlelist($page=0){
		if ($this->auth->has_rights_to("list","article"))
		{
			if ($page < 0) { $page = 0;}
			
			$this->load->library('paginationajax');
			
			$config['base_url'] = site_url('article/articlelist/');
			$config['total_rows'] = $this->Article_model->get_article_count();
			$config['per_page'] = '2';
			$config['js_function'] = 'update_news';
		
			$data['articlelist']=$this->Article_model->get_articles($page, $config['per_page']);		
			$this->paginationajax->initialize($config);
			$data['pagination']=$this->paginationajax->create_links();
			$data['short']=false;
			
			$this->load->view('article/list',$data);
		}
		else
		{
     		$this->load->view('forbidden');
		}
	}
	
	/**
	 * Deletes the article
	 */
	function delete($art_id) {
		if ($this->auth->has_rights_to("delete","article", $art_id))
		{
			$this->Article_model->delete_article($art_id);
		}
		else
		{
     		echo 'You haven\'t got the right to delete this article';
		}
	}
	
	/**
	 * Display the edit article view in order to edit the article $id
	 */
	function edit($id){
		
		if ($this->auth->has_rights_to("edit","article", $id))
		{
			if(is_Numeric($id))
				$this->Article_model->load_article_from_id($id);
			
			$data = $this->Article_model->data;
			if(is_array($data))
				$this->load->view('article/edit',$data);
				else
				$this->load->view('forbidden');
		}
		else
		{
     		$this->load->view('forbidden');
		}
	}
	
	/**
	 * Save the new information of the article into the database
	 */
	function edit_article($id){
		if ($this->auth->has_rights_to("edit","article", $id))
		{
			$infos = array (
							'title' => $this->input->post('title'),
							'content' => $this->input->post('text_content')
			);
			
			if (!$this->Article_model->save_article($id,$infos))
				$this->load->view('system_error');
     		else
     			$this->load->view('editOk');
		}
		else
		{
     		$this->load->view('forbidden');
		}
	}
}
?>
